package com.neuedu.projectedu.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.neuedu.projectedu.entity.Menu;
import com.neuedu.projectedu.entity.User;
import com.neuedu.projectedu.mapper.MenuMapper;
import com.neuedu.projectedu.mapper.UserMapper;
import com.neuedu.projectedu.service.MenuService;
import com.neuedu.projectedu.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceImpl extends ServiceImpl<MenuMapper, Menu> implements MenuService {

    @Override
    public List<Map> getMenus() {
        List<Menu> menuList=this.list();
        List<Map> menus=new ArrayList<>();
        for (Menu menu:menuList) {
            if(null==menu.getParentId()||0==menu.getParentId()){
                Map map=new HashMap();
                map.put("id",menu.getId());
                map.put("title",menu.getMenuName());
                map.put("children",getSubMenu(menuList,menu.getId()));
                menus.add(map);
            }
        }
        return menus;
    }

    public List<Map> getSubMenu( List<Menu> menuList,Long parentId){
        List<Map> subMenu=new ArrayList<>();
        for (Menu  menu:menuList) {
            if(menu.getParentId()!=null&&menu.getParentId().equals(parentId)){
                Map map=new HashMap();
                map.put("id",menu.getId());
                map.put("title",menu.getMenuName());
                subMenu.add(map);
            }
        }
        return subMenu;
    }
}
